package com.serene.authority.admin.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serene.authority.admin.entity.MyDict;
import com.serene.authority.admin.entity.MyDictDetail;
import com.serene.authority.admin.mapper.DictDetailMapper;
import com.serene.authority.admin.service.DictDetailService;
import com.serene.authority.admin.service.DictService;
import com.serene.authority.common.utils.Result;
import com.serene.authority.common.utils.ResultCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateTime.now;

@Service
public class DictDetailServiceImpl extends ServiceImpl<DictDetailMapper, MyDictDetail> implements DictDetailService {
    @Autowired
    private DictService dictService;

    @Autowired
    private DictDetailMapper dictDetailMapper;

    @Override
    public Result<MyDictDetail> getDictByName(Integer offectPosition, Integer limit, String dictName) {
        MyDict dictByName = dictService.getDictByName(dictName);
        Integer dictId = dictByName.getDictId();
        Page page = PageHelper.offsetPage(offectPosition, limit);
        List<MyDictDetail> fuzzyDictDetailByPage = getDictDetail(dictId);
        return Result.ok().count(page.getTotal()).data(fuzzyDictDetailByPage).code(ResultCode.TABLE_SUCCESS);
    }

    @Override
    public List<MyDictDetail> getDictDetail(Integer dictId) {
        QueryWrapper<MyDictDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", dictId);
        return dictDetailMapper.selectList(wrapper);
    }

    @Override
    public Integer insertDictDetail(MyDictDetail myDictDetail) {
        myDictDetail.setCreateTime(new Date());
        myDictDetail.setUpdateTime(new Date());
        return dictDetailMapper.insert(myDictDetail);
    }

    @Override
    public MyDictDetail getDictDetailById(Integer id) {
        QueryWrapper<MyDictDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return dictDetailMapper.selectOne(wrapper);
    }

    @Override
    public int updateDictDetail(MyDictDetail myDictDetail) {
        return dictDetailMapper.updateDictDetail(myDictDetail);
    }

    @Override
    public int deleteDictDetailByIds(String ids) {
        Integer[] dictDetailIds = Convert.toIntArray(ids);
        return dictDetailMapper.deleteDictDetailByIds(dictDetailIds);
    }

    @Override
    public int deleteDictDetailByDictId(Integer dictId) {
        return deleteDictDetailById(dictId);
    }

    @Override
    public Integer deleteDictDetailById(Integer dictId) {
        QueryWrapper<MyDictDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", dictId);
        return dictDetailMapper.delete(wrapper);
    }
}
